package com.koron.css2.systemManage.servlet;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.systemManage.bean.OperateBean;
import com.koron.css2.systemManage.service.IOperationService;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.PingYin;
import org.swan.bean.MessageBean;

/**
 * 菜单修改
 * @author 59532
 *
 */
public class UpdateOperation implements ServerInterface{
	
	private static Logger log = LogManager.getLogger(UpdateOperation.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<Void> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Void.class);
		/*try {
			OperationService operationService = new OperationService();
			SysModuleMapper moduleMapper = factory.getMapper(SysModuleMapper.class);
			List<SysModuleBean> sysModules = moduleMapper.selectSysModule(new SysModuleBean());
			OperationQueryBean query = new OperationQueryBean();
			query.setApp(ApplicationConfig.getAppCustomerAppid());
			List<OperationTreeBean> operations = operationService.listAllOperation(query);
			for(SysModuleBean module : sysModules) {
				for(OperationTreeBean operation : operations) {
					if(module.getModuleName().equals(operation.getOpName())) {
						OperateBean operate = new OperateBean();
						if(StringUtils.isNotEmpty(module.getPageAddr())) {
							operate.setUrl(module.getPageAddr());
						}
						if(StringUtils.isNotEmpty(module.getShortcutIcon())) {
							operate.setIcon(module.getShortcutIcon());
						}
						operate.setApp(ApplicationConfig.getAppCustomerAppid());
						operate.setOpFlag(1);
						operate.setOpName(operation.getOpName());
						operate.setOpStatus(1);
						operate.setOpCode(operation.getOpCode());
						operate.setOpWeight(operation.getOpWeight());
						operationService.updateOperation(operate);
					}
				}
			}
		} catch (Exception e) {
			log.error("修改菜单出错：" + e);
			msg.setCode(Constant.MESSAGE_INT_FAIL);
			msg.setDescription("修改菜单出错");
		}*/
		try {
			OperateBean operate = JsonUtils.objectToPojo(req.getData(), OperateBean.class);
			if(StringUtils.isEmpty(operate.getOpName())) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("菜单名称不能为空");
				return msg;
			}
			if(operate.getOpFlag() == null) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("菜单类型不能为空");
				return msg;
			}
			if(StringUtils.isEmpty(operate.getSymbol())) {
				operate.setSymbol(PingYin.getInstance().getSelling(operate.getOpName(), false, ""));
			}
			IOperationService operationService = ServiceFactory.getOperationService();
			OperateBean operation = new OperateBean();

			operation.setApp(ApplicationConfig.getAppCustomerAppid());
			operation.setOpFlag(operate.getOpFlag());
			operation.setOpName(operate.getOpName());
			operation.setUrl(operate.getUrl());
			operation.setOpStatus(1);
			if(StringUtils.isNotEmpty(operate.getIcon())) 
				operation.setIcon(operate.getIcon());
			operation.setOpCode(operate.getOpCode());
			operation.setOpWeight(operate.getOpWeight());
			operationService.updateOperation(operation);
		} catch (Exception e) {
			log.error("修改菜单出错：" + e);
			msg.setCode(Constant.MESSAGE_INT_FAIL);
			msg.setDescription(e.getMessage());
		}
		return msg;
	}

}
